Observing properties associated with an object in an object-oriented programming platform

ABSTRACT

An application programming interface is provided in an object-oriented programming platform for developing applications. A method comprises registering a first object with a second object to observe one or more properties associated with the second object, the second object having a set of methods associated therewith and replacing a first implementation of a method in the set of methods associated with the second object with a second implementation of the method that automatically sends a notification to the first object. The observable objects need not explicitly invoke the observer notifications, avoiding writing of a large amount of code. These observer notifications need only be sent for objects that are actually being observed but are related objects, substantially reducing the performance overhead of needless invocations of the observer notifications without limiting binding between objects.

1. FIELD OF THE INVENTION

The invention generally relates to computer programming technique tooperate object- oriented software programs, and more particularly, toproviding an application programming interface in an object-orientedprogramming platform to observe properties associated with an object.

2. DESCRIPTION OF THE RELATED ART

Many computer programming languages are used for development of softwareprograms. Object-oriented programming is popular since it offersprogramming flexibility with a relatively simple structure.Object-oriented programming is a simple way of creating softwareprograms using building blocks, and forming relationships for thesebuilding blocks.

Such building blocks in object-oriented computing systems are generallycalled “objects.” An object is a programming unit or a software entitythat combines a data structure (e.g., instance variables) with theoperations that can operate on that data. In object-orientedprogramming, these operations that can be performed on the data aregenerally called “methods.”

For example, an object-oriented program comprises a collection ofsoftware entities known as objects, each of which is responsible forperforming a particular task. Every object is characterized byassociated properties, or data values, and one or more methods thatprovide certain behavior or functionality. In operation, one objectinvokes the methods of another object, to cause the second object toexhibit its associated behavior.

Using objects, an object-oriented software application may define itsdata and the methods that operate on the data. An object may communicatewith another object by sending messages. For example, when one objectdesires to operate on data that resides in another object or needs toexecute methods in another object, it sends a message. The requestingobject identifies the intended operation by specifying a method. Therequesting object can further send arguments that may be used by theresponding object in performing the named method. The responding objectperforms the method requested in the message. In this manner, the dataprovided by the requesting object may be used to perform the indicatedoperation by the method.

When a programmer defines a method, he or she specifies the classes oridentities of the arguments appropriate for the method. A class is aprogramming language concept that allows data and methods to be groupedtogether. The class concept is fundamental to the notion of anobject-oriented programming language. The methods of a class define theset of permitted operations on the class's data (its attributes). Thisclose tie between data and operations means that an instance of aclass—an object—is responsible for responding to messages defined by itsclass. As a basic building block of object-oriented programming, classesdefine types of objects by specifying their properties (data) and theprocedures (“methods”) they support.

Programming in an object-oriented computing system requires theprogrammer to build a structure for the set of objects, which defineshow properties and methods are to be manipulated. For example,developing applications with user interface programming, such astwo-dimensional drawing techniques require efficient observationinformation storage and manipulation when working with objects. However,designing such a structure can not only be a complex and difficult taskbut a relatively inefficient one because of the overhead associated withobject interactions, sending a large number of messages andnotifications back and forth between the objects.

In developing applications with user interface programming, objects mayneed to be notified of changes to specified properties of other objectsby accessing an object's properties indirectly. However, one problem isthat observable objects explicitly invoke the observer notifications,requiring a large amount of code which must be written. A secondaryproblem is that part of the performance cost of sending observernotifications is borne not only by objects that are actually beingobserved but other related objects as well which are not observed, whichmeans that the performance overhead of needless invocations of theobserver notification machinery is relatively high. Moreover, anobserver may have to inspect the entirety of the related objectscollection to find out what objects have been inserted, removed, orreplaced.

In an Operating System (OS) environment, a binding feature provides anability to easily connect user interface items to model code being theclasses which encapsulate domain specific information and theirapplication. This binding feature uses a mechanism called “key-valueobserving” that allows objects to be notified of changes to specifiedproperties of other objects. The key-value observing makes objects toeasily bindable. For example, a graphical user interface (GUI), such aninterface builder, may be used to bind text fields, checked boxes andtable views to controller objects and model objects. However, someobservation systems using observer notifications can indicate thatemployee objects are related to a manager object but after a change inmanager-employee relationships, it is difficult to determine whichemployee objects become related or unrelated to the manager object.

The present invention is directed to overcoming or at least reducing oneor more of the problems set forth above.

SUMMARY OF THE INVENTION

In one aspect of the instant invention, a method is provided forproviding an application programming interface in an object-orientedprogramming platform. The method comprises registering a first objectwith a second object to observe one or more properties associated withthe second object, the second object having a set of methods associatedtherewith and replacing a first implementation of a method in the set ofmethods associated with the second object with a second implementationof the method that automatically sends a notification to the firstobject.

In another aspect of the instant invention, an article comprising acomputer readable storage medium storing instructions is provided. Theinstructions that, when executed cause a processor-based system toregister a first object with a second object to observe one or moreproperties associated with the second object, the second object having aset of methods associated therewith and replace a first implementationof a method in the set of methods associated with the second object witha second implementation of the method that automatically sends anotification to the first object.

In yet another aspect of the instant invention, an object-orientedcomputing system is provided. The system comprises a processor, a memorycoupled to the processor, the memory storing a data file structureincluding an identification of a first and a second object in a softwareprogram, one or more properties and a set of methods associated withrespective the first and second objects and instructions for registeringthe first object with the second object to observe the one or moreproperties associated with the second object, and an interface toreplace a first implementation of a method in the set of methodsassociated with the second object with a second implementation of themethod that automatically sends a notification to the first object.

In still another aspect of the instant invention, an applicationprogramming interface for managing relationships of objects in anobject-oriented computing system is provided. The applicationprogramming interface comprises means for registering a first objectwith a second object to observe one or more properties associated withthe second object, the second object having a set of methods associatedtherewith and means for replacing a first implementation of a method inthe set of methods associated with the second object with a secondimplementation of the method that automatically sends a notification tothe first object.

BRIEF DESCRIPTION OF THE DRAWINGS

The invention may be understood by reference to the followingdescription taken in conjunction with the accompanying drawings, inwhich like reference numerals identify like elements, and in which:

FIG. 1 illustrates an object-oriented computing system according to oneillustrative embodiment of the present invention;

FIG. 2 illustrates an application programming interface for use with theobject- oriented computing system shown in FIG. 1 in accordance with oneillustrative embodiment of the present invention;

FIG. 3 illustrates a stylized representation of a method for key-valueobserving based on registering and deregistering of an object withanother object in accordance with one illustrative embodiment of thepresent invention;

FIG. 4 illustrates a model-view-controller architecture for userinterface programming of the application programming interface shown inFIG. 2 according to one illustrative embodiment of the presentinvention;

FIG. 5 illustrates a stylized representation of a method for key-valueobserving for user interface programming of the application programminginterface shown in FIG. 2 in accordance with one illustrative embodimentof the present invention;

FIG. 6 illustrates a method for providing an automatic key-valueobserving notification for user interface programming of the applicationprogramming interface shown in FIG. 2 according to one illustrativeembodiment of the present invention;

FIG. 7 illustrates a stylized representation of a method for generatinga single notification for multiple objects in accordance with oneillustrative embodiment of the present invention; and

FIG. 8 illustrates a stylized representation of a method for observing aproperty by observing an attribute identified by a key path inaccordance with one illustrative embodiment of the present invention.

DETAILED DESCRIPTION OF SPECIFIC EMBODIMENTS

Illustrative embodiments of the invention are described below. In theinterest of clarity, not all features of an actual implementation aredescribed in this specification. It will of course be appreciated thatin the development of any such actual embodiment, numerousimplementation-specific decisions must be made to achieve thedevelopers' specific goals, such as compliance with system-related andbusiness-related constraints, which will vary from one implementation toanother. Moreover, it will be appreciated that such a development effortmight be complex and time-consuming, but would nevertheless be a routineundertaking for those of ordinary skill in the art having the benefit ofthis disclosure.

Embodiments of the present invention provides for an applicationprogramming interface for use with an object-oriented computing system.The application programming interface uses a key-value observingmechanism by which objects may be registered and deregistered asobservers of specific properties of other objects, and by which observedobjects may notify observers of changes to properties according to oneembodiment of the present invention. The key-value observing mechanismis based on a key-value coding mechanism which provides for executing anobject's properties indirectly. That is, using strings to identifyproperties, rather than through invocation of an accessor method or asinstance variables.

Referring to FIG. 1, an object-oriented computing system 100 is shownaccording to one embodiment of the present invention. Theobject-oriented computing system 100 may comprise a processor 105coupled to a memory 110 storing a data file structure 115 including anidentification of an observer object 120 and an observed object 125 in asoftware program, such as an operating system program 130. The memory110 may store one or more properties and a set of methods associatedwith the respective observer object 120 and the observed object 125. Theoperating system program 130 may provide instructions for registeringthe observer object 120 with the observed object 125 to observe one ormore properties thereof

In one embodiment, the operating system program 130 may comprise aplurality of application programming interfaces (APIs) which may beseparated into frameworks. These frameworks may include a Foundationframework 135 that resides on a core foundation 140. While theFoundation framework 135 may enable a binding feature for objects in anobject- oriented environment, the core foundation 140 implementsconventional APIs. The core foundation 140 may reside on other APIs 145,such as standard UNIX-based APIs. Over the Foundation framework 135, anapplication kit (AppKit) framework 150 may reside. Other frameworks mayreside over the AppKit framework 150.

In Macintosh Operating System (Mac OS), such as Mac OS X, applicationprogramming interfaces (APIs) are divided into frameworks including theFoundation framework 135 which supports the AppKit framework 150. TheFoundation framework 135 and the AppKit framework 150 may be implementedin any of several available programming languages, such as Objective-C,an object-oriented programming language. This Foundation framework 135may be used with a binding feature introduced earlier. However, theFoundation framework 135 may be used with other features available to anOperating System environment. In the Foundation framework 135, a classhierarchy is based on a single super class called an object class. Basedon categories, the Foundation framework 135 may be implemented using theobject class. A category is a feature of the Objective-C which enablesaddition of methods to existing classes.

In the Objective-C language, for example, a class defines the methodsand instance variables for a particular kind of object. A classdefinition declares instance variables and defines methods for allmembers of the class. Objects that have the same types of instancevariables and have access to the same methods belong to the same class.In the Objective-C language, a class object or object class is an objectthat represents a class and knows how to create new instances of theclass. Class objects are created by the compiler, lack instancevariables, and can't be statically typed, but otherwise behave like allother objects. As the receiver in a message expression, a class objectis represented by the class name. In the

Objective-C language, a class method is a method that can operate onclass objects rather than instances of the class.

The memory 110 may couple an input/output (I/O) circuitry 165 to theprocessor 105. In turn, the I/O circuitry 165 may couple one or moreperipherals 170, a mass storage device 175, and a display device 180, tothe memory I/O in one embodiment. The mass storage device 175 may storea database 185 in which each object of the observer object 120 and theobserved object 125 forms the basis for a record 190. Each record 190may contain fields which respectively identify the pointers and themethods with which the record is associated.

In the display device 180, the object-oriented computing system 100 mayprovide an interface 195 to replace a first implementation of a methodin the set of methods of the observed object 125 with a secondimplementation of the method that automatically invokes a notificationto the observer object 120 from the observed object 125. Instead of anautomatic observer notification, manual notifications may be provided insome embodiments of the present invention. The notifications are sentdirectly to the observer object 120 when changes are made to specificproperties of the observed object 125. To provide an automatic observingcapability for objects, a window 197 may be displayed on the displayeddevice 180 of the object-oriented computing system 100.

Turning now to FIG. 2, an application programming interface (API) in theform of the Foundation framework 135 is shown according to oneembodiment of the present invention. The Foundation framework 135comprises a key-value coding module 200 and a key-value observing module205. The AppKit framework 150 comprises a key-value binding module 210.The key-value coding module 200 gives access to object properties usinga very generic application programming interface (API) that identifiesthem by a key. In one embodiment, a key is a string, such as “graphics,”or “fillColor.” An object property may be an attribute, to-onerelationship, or to-many relationship. The attribute may be a simpleimmutable value, for example, the fillColor or stroke width of a Sketchgraphic. A to-one related object may have properties of its own, e.g.,the image of a Sketch image graphic. The to-many related objects may bea collection of related objects, e.g., graphics of a Sketch document.

Using the key-value coding module 200 and the key-value observing module205, the key-value binding module 210 provides an applicationprogramming interface that allows the value of an object's property tobe tied to the value of another object's property. In this manner, theobject that supports this binding feature may provide custom options,providing plenty of flexibility in implementation.

The observed object 125 responds to key-value observer messages becauseits class inherits keys-value observer methods defined by an objectclass from the Foundation framework 135 in the key-value observingmodule 205. The observer object 120 may observe one or more objectproperties of the observed object 125 including simple attributes,to-one relationship, and too-many relationships. The observer object 120of to-many relationships may be informed of the type of change made to aspecific property of the observed object 125 as well as which objectsare involved in the change.

In order to be notified of changes to a property, the observer object120 first registers with the observed object 125 by sending a message,passing the observer object 120 a key path of a property that will beobserved. In one embodiment, a key path may be a dot-separated list ofkeys which get handled automatically. For example, a key path, such as“selection.fillColor” may be passed. An observed class associated withthe observed object 125 may be key-value observing compliant for theproperty that the observer object 120 wishes to observe.

For the key-value observing module 205, which is an applicationprogramming interface, to observe objects, the classes of the objectsmust be key-value observing (KVO)—compliant (observable) for otherobjects to bind to them. That is, either using manual observernotification or an automatic observer notification, the objects may bemade KVO compliant. While invocations of key-value coding (KVC) methodsmay result in observer notifications, the notifications of KVC—compliantmethods may result in observer notifications as well. To be manualKVO—compliant, the observed object 125 before any change to an attributeor to-one relationship invokes a method “will change value for key” andafter the change, the observed object 125 invokes a method “did changevalue for key.” Likewise, before any change to a to-many relationship,the observed object 125 invokes a method “will change: values atindexes: for key.”

For the automatic KVO-compliance, the observed object 125 causesobserver notifications for a key in message, such as “set value: forkey”. Likewise, a method naming pattern “-setKey:” causes observernotifications. For example, invoking a method named “- setFillColor:”automatically result in KVO notifications, unless automatic KVO isdisabled for the “fillColor” key. Moreover, mutations of the result of amethod naming pattern “- mutableArrayValueForKey:” causes notificationsfor that key. In addition, method naming patterns“-insertObject:InKeyAtIndex” and “-removeObjectFromKeyAtIndex” causeobserver notifications for the key.

The Foundation framework 135 may further comprise an object class 215and a set of associated object methods 220. In addition, the Foundationframework 135 may comprise a standard collection class, such as an arrayclass 225 having a set of associated array methods 230, in oneembodiment. In the Foundation framework 135, besides the object class215 the array class 225 may be provided. The array class 225encapsulates an array of objects. While the object methods 220 for theobject class 215 may include a method “value for key” which returns thevalue of the “key” property, the array methods 230 include a firstmethod called “add observer: to objects at indexes: for key path:options: context:” and “remove observer: from objects at indexes: forkey path:”

In one embodiment, the “value for key” method searches for the objectclass 215 for a method of the same name, and invokes it. The “value forkey” method also returns the value of the key property for to-manyrelationships where the related objects are not stored in the arrayclass 225. The object methods 220 may further include a “set value: forkey” method which sets the value of the key property for to-manyrelationships where the related objects are not stored in the arrayclass 225. The object methods 220 may further include a “set value: forkey” method which sets the value of the key property. Again, the “setvalue: for key” method, searches, the object class 215 for a methodwhose name matches, and invokes it.

The object methods 220 may include a “validate value: for key: error”method, which either returns a value good for a “set value: for key”method or returns an error, separating validation rules from decisionabout when to validate. Likewise, the object methods 220 may include a“mutable array value for key” method which returns a mutable array ofto-many related objects. For each object method 220 there is a variantthat uses a key path being a dot —separated list of keys which gethandled automatically.

Using the key paths, the key-value coding module 200, enables key-valuebinding and key-value observing in terms of key paths. For making theobject class 215 and the array class 225 KVC-compliant, the objectmethods 220 and the array methods 230 include methods “value for key”and “set value: for key.” For each attribute or to-one relationship,method name “key” and for read write a method named “set key” maybeimplemented. For each to-many relationship, likewise, a method named“key”, “count of key”, and “object in key at index” may be implemented.For each mutable to-many relationship, a method called “insert object:in key at index” and a method called “remove object from key at index”may be implemented. In all of the above indicated methods, the word“key” in each method name may be replaced by the actual key.

For key-value observer registration, the object methods 220 may includea method called “add observer: for key path: options: context” toregister one object as an observer of a property of another object.Likewise, to deregister the observer object 120, a method called “removeobserver: for key path may be provided. In the “add observer: for keypath: options: context” method, the options may comprise any combinationof two or more possible flags so that the observer object 120 may get anew value for the property in the change notification and may get an oldvalue from the observed object 125 before the change.

Likewise, the context defines a reason for observation which is passedback in a key value observing message. The observed object 125 containsthe property to be observed and made KVO compliant for the key path. Forthe “add observer: for key path: options: context” method, the recipientof the notifications about changes to the property identified by the keypath is the observer object 120 that may implement a key-value observinginformal protocol. For example, the controls in a panel such as Sketch'sinspector panel may observe the selection properties which they arebound.

For the key-value observing module 205, the object methods 200 include amethod called “observe value for key path: of object: change: context,”which is invoked on the observer object 120 for every change to theobserved property in the observed object 125. The change parameter inthis method may indicate information about what changed. A key valuechange kind indication for the method “observe value for key path: ofobject: change: context” may indicate the kind of change it was. Forexample, the value of an attribute or to- one relationship may bechanged, when someone invoked a method called “set value: for key”.Likewise, a to-many relationship may have changed when someone invoked amethod called “mutable array value for key” then mutated the result.Additionally, a key value change indexes key indication may indicate theindexes of the inserted/removed/replaced objects.

The key-value observing (KVO) module 205 may be built on the key-valuecoding (KVC) module 200. Using the key-value coding module 200, amessage may be sent to an object without the knowledge of a name of amethod in the object's class 215 before getting and setting the valuesof the attributes in that object. The key-value coding module 200comprises a value for key method and a set value for key method. Thevalue for key method takes one argument and the key is a string. Thestring is the key of the property whose value is desired. In this way,properties of an object may be extracted without knowing how theobject's class 215 implements the property getting and setting.

When the value for key method is invoked, the key-value coding module200 searches for a method in the object's class whose name is “key.” Ifthe key method is not located, then the key-value coding module 200searches for another method called “get key.” Accordingly, if a methodis not found, the key-value coding module 200 searches for instancevariables for the object. For example, when sending an employee object avalue for key method where key is “manager”, the key-value coding module200 attempts to execute a method named manager and if the manager methodis determined to be not present, then the key-value coding module 200searches for an instance variable whose name is “manager.” The key-valuecoding module 200 obtains a value from the instance variable of themanager object and returns the value. In this way, the key-value codingmodule 200 protects clients from having to know how the properties areimplemented, providing implementation freedom to key-value coding module200 compliant classes because an instance variable is hidden behind amethod.

When properties of an object, such as that of an employee class needs tobe observed, for example, observing the manager property of the employeeobject having a method named “set manager,” the creator of the employeeclass may make the employee object key-value coding compliant withoutdoing anything else. That is, the employee class is automaticallykey-value observing compliant for the manager property as long as themanager is changed only via invocation of the “set manager” method.

In general, the key-value observing module 205 is built on the key-valuecoding module 200. One of the features of a method called “set value forkey”, which is part of the key-value coding module 200, is that if amethod cannot be found whose name matches the pattern, the “set valuefor key” method may search for an instance variable whose name matchesthe pattern. In this event, the “set value for key” method will changethe value of the instance variable directly. When the “set value forkey” method is invoked, an automatic key value observing (KVO)notification feature operates. The key-value coding module 200 is madeaware of the key-value observing module 205, e.g., if the key-valuecoding is going to change the value of the instance variable directly, a“will change value for key” method before the key value is changed and a“did change value for key” method after the key value is changed arecalled. Thus, an automatic key-value observer notification in responseto key-value coding messages is provided.

A property may be changed in at least two ways. In the first way, anaccessor method, such as a set key method may be called. In the secondway, a generic method may be called which identifies the property of thekey, i.e., a set value for key.

A relationship may be mutated using a mutable array value for keymethod, e.g., when one object is mutating another object, i.e., addingor removing objects from one of the objects to-many relationships mayoccur. For example, by calling the mutable array value for key method,to-many relationships of an object may be changed, e.g., the number ofemployee objects related or unrelated to the manager object may bealtered. The mutable array value for key method returns, an instance ofa standard array class, to add, remove or replace objects in the arrayby automatically relating and unrelating objects to the container objectthat was originally sent the mutable array value for key message. Whileautomatic relating and unrelating of objects continues, automatic KVOnotifications may be generated. In this manner, one or more propertiesof an object may be changed by a specific method implemented by theobject's class directly or by a generic method based on the key-valuecoding module 200, while the automatic KVO notifications may begenerated for both of these methods.

Referring to FIG. 3, a stylized representation of a key value observingmethod based on registering and deregistering of an object with anotherobject is shown according to one embodiment of the present invention.Using the Foundation framework 135 shown in FIG. 2, the observer object120 may register 300 with the observed object 125. Likewise, at somepoint, the observer object 120 may deregister 305 with the observedobject 125. The observed object 125 may include one or more properties310, such as a manager object 315. Additionally, the observed object 125may have a set of associated methods 320 including an implementation ofan original method 325. Using the Foundation framework 135 illustratedin FIG. 2, a replacement method 330 implementation may replace theoriginal method 325 implementation. The replacement method 330implementation may comprise a first method (e.g., a will change method)335 and a second method (e.g., a did change method) 340.

In this manner, based on method naming rules when a set manager methodcall is invoked, the observer object 120 is automatically notified. Inone embodiment, using a feature in Objective-C, i.e., something is anobject of something else; the key-value observing module 200 utilizes astructure by pointing to it in a particular order to determineinformation relating to various methods that are available for theobject class 215 of that object. Therefore, when the observer object 120observes another object and an automatic key-value observing (KVO)notification feature is on, the key-value observing module 200 makes acopy of the class of the observed object 125 and replaces one of themethods in a list of methods of the observed object 125 with anothermethod that sends out notifications while performing the same operationas the original (replaced) method was intended to perform.

Even when the property that is being changed possesses to-manyrelationships to one or more objects, notifications of a change may besent out automatically as objects become related and/or unrelated toanother object. This avoids writing of a relatively large piece of codefor converting the objects into observable objects. Thus, in oneembodiment, the observed object 125 does not need to include anyadditional code for being observed as long as a naming patternestablished by the key-value coding module 200 is followed.

In operation, for one object to observe another object, the observerobject 120 registers with the observed object 125. An object sends amethod to another method, e.g., called “add observer for key path:options and contexts” where options are getting old and new values aspart of the observer notification and context enables passing thesevalues back to the invoking object. To add an observer, a pointer ispassed to an object which is going to be observed. A key path, which isa series of property names separated by dots that get passed around inthe APIs when dealing with keys in the context of key-value coding, maybe used by the observer.

For example, a person record 190 of a person object may have a to-onerelationship to an address object having an attribute as a zip code. Thekey path in this case would be address.zipcode. By observing theaddress.zipcode, the zip code of a person may be observed even thoughthe person object does not have the zip code attribute. An “add observerfor key path” method may be sent to the observed object 125, registeringone object as an observer for another object. However, an object doesnot have to register itself an observer because one object can registeranother object as an observer for the observer object 120. A “removeobserver for key path” method operates exactly opposite that of “addobserver for key path” method, deregistering an object as an observer.Using these two methods, an object registers 300 and deregisters 305 asan observer for another object, respectively.

The observer object 120 implements a method named “observe value for keypath change context.” When a property of the observed object 125changes, a notification is sent out to each one of the observer objectsthat identifies the key path of the property that was observed and nowchanging, the object that owns the property that is changing,information on exactly what changed, and the context which is the valuespecified when an object registered as an observer and that value ispassed back to the observer. Thus, the observer object 120 registersitself and listens for notifications.

To be an observed object, the observed object 125 may be devised to bekey-value observing (KVO) compliant for a specific property that may beobserved by an observer object. A key-value observing compliant observedobject follows certain conventions for a specific property. For example,when using an automatic KVO notification, by ensuring that any changesmade to the observed object's properties 31,0 goes to the set methodthat has the correct name, set manager for the employee object.

When, the manager property 315 changes, the change is done through setmethod to make the observed object 125 KVO compliant. In this manner,any object can observe the manager property 315 of the observed object125 and the observer object 120 gets notified automatically when themanage property 315 changes. For an object to be KVO compliant entailsthat before an object's value is going to be changed, a call is made toa method called “will change” value for key 335 and after the change ismade a call is made to a method called “did change” value for key 340where key being the manager property 315. The automatic KVO featureinvokes these both methods 335, 340 automatically at an appropriatetime, in one embodiment.

By sending a method to a class instead of an instance of a class, aclass may be allowed to declare that the value of one or more of itsproperties is inherently dependent or affected by the value of one ormore of its other properties. For example, when an application isstarting up, a program can declare that value of one property dependsupon another property. In this manner, whenever the value of oneproperty changes, an assumption may be made that the value of the otherproperty changed as well. An observer notification may be sentindicating this change.

Instead of calling the first method, i.e., the will change value for keymethod 335 and the second method, i.e., the did change value for keymethod 340 for a model object with a class called person having a firstname and a last name and a full name property it can be declared thatthe persons full name depends upon the first name and last name sowhenever the person class performs the will change and did change valuefor key methods 335 and 340 for the first name or the last name, theobserver of the objects full name gets notified that the full name ischanged. That is, when a set first name method is invoked, observers ofthe full name may be automatically notified that the full name ischanged.

When one object registers itself as an observer of another object, theobserver object 120 may specify that a notification from the observedobject is desired immediately upon the registration. In this way, usingan option for an immediate notification, any tasks the observer object120 must perform at initial observer registration time as well as whenthe observed property changes can be consolidated in one place in theimplementation of the observer object's class. When using an observevalue key path method, e.g., the observer object 120 arranges so thatthe observer value key path method may be called automatically becausethe observer object 120 is registering itself as an observer of theobserved object 125. That is, without executing any additional code theobserver value key path method may be called. Upon receiving anotification that one or more objects have become related or unrelated,the observer object 120 may selectively choose to observe the propertiesof now related objects based on the option for an immediate notificationin one place.

Referring to FIG. 4, a model-view-controller architecture 400 for userinterface programming is depicted for the object-oriented computingsystem 100 shown in FIG. 1 according to one embodiment of the presentinvention. The model-view-controller architecture 400 may comprise oneor more controllers 405, one or more model objects 410 and associatedviews 415. The controllers 405 may be classes, such as array controllerclass, object controller class, and user defaults controller class. Themodel objects 410 may be objects that are KVC-compliant for otherobjects to bind to them. An example of the views 415 may be a tableview.

The key-value coding module 200 enables the views 415 to get and setvalues in the controller's 405. In addition, the key-value coding module200 enables the controllers 405 to get and set values in the modelobjects 410. The key-value observing module 205 enables the views 415 benotified of changes to values in the controllers 405. Moreover, thekey-value observing module 205 enables the controllers 405 be notifiedof changes to values in the model object 410. The key-value bindingmodule 210 binds the named properties of the views 415 and thecontrollers 405 to the properties in the controllers 405 and the modelobject 410. This binding enables observing using the key-value observingmodule 205. The binding also enables updating of bound-to values inresponse to user actions using the key-value coding module 200.

For example, a table view's contents may be bound to an arraycontroller's “arranged objects.” The array controller's contents arraymay in turn be bound to a to-many relationship of a model object. Inthis manner, the array controller class may manage the life cycle of anarray of objects. For instance, the KVO notifications from the modelobjects 410 may cause KVO notifications to the corresponding table views415.

As one example, for a class called “an array controller” a table viewmay be bound to it, i.e., the table view receives values from the arraycontroller class. The array controller class may be bound with a to-manyrelationship to other objects. Because of this binding of the contentsof the array controller class, the array controller class passes therelated objects to the table view using the key-value coding module 200and the key-value observing module 205. In this manner, the arraycontroller class may potentially observe the related objects.

The array controller class may include a feature, i.e., when a userselects many objects in the table view, the feature exposes a propertycalled “selection” for values that are common to each of the rows in thetable view. Based on a master-detail relationship, an inspector window,such as the window 197 may show properties of the selected objectsbecause the array controller class observes each of the related objects.If all of the related objects have an exact same value, then that valueappears in the inspector window for a property. Otherwise, if forselected rows of the table view the related objects possess differentvalues, then in the inspector window a place holder appears, indicatingthe presence of multiple values in a user interface. For registrationand deregistration as an observer of the related objects, the arraycontroller class may accomplish common overhead associated with observerregistration and deregistration once for the related objects and applythe same over again and again to each observed object.

Using the array controller class, the key-value binding module 210 maybind the “selection indexes” of a table view to an array controller's“selection indexes”. Likewise, the properties of user interface controlsmay be bound to a key path, such as “selection.modelKey”. Changes to theselection indexes may cause KVO notifications for every substantiallyobserved key path selection.modelKey. In addition, the key-value bindingmodule 210 may bind the properties of user interface controls, to“selection.modelKey” of an object controller class. The key-valuebinding module 210 may bind the object controller's “objectrelationship” to a to-one relationship of a model object, or anothercontroller. In this manner, the object controller class may manage thelife cycle of an object. Using the key-value binding module 210, a userdefaults controller class may bind the values of user interface controlsto “value.preferencesKey” of a user defaults controller. The userdefaults controller class provides an easy access to up-to-datepreferences values via the key-value observing module 205 to other partsof an application.

Referring to FIG. 5, a stylized representation of a method for key-valueobserving in accordance with one embodiment of the present invention isshown. At block 500, an application programming interface, such as theFoundation framework 135 and the AppKit framework 150 shown in FIG. 2may be provided in an object-oriented platform on a system, such as onthe object-oriented computing system 100 shown in FIG. 1. At block 505,a first object (e.g., the observer object 120) may register with asecond object (e.g., the observed object 125) to observe one or moreproperties thereof. The observed object 125 may have associated the setof associated methods 320, as shown in FIG. 2. At block 510, theoriginal method 325 implementation may be replaced with the replacementmethod 330 implementation of a method from the set of methods 320. Atblock 515, a notification may be invoked automatically to the observerobject 120 from the observed object 125. The notification may indicate achange to one or more properties of the observed object 125.

The method implementation replacement that is used to implementautomatic KVO notification may be implemented in two ways in someembodiments of the present invention. The first implementation involveschanging the object's class pointer (the “isa,” in the Objective-Clanguage) to point to a newly created class that is merely a copy of theobject's original class, and then mutating the class copy's method list.This is how KVO may be implemented for performance because there is nocost attributable to the KVO autonotification feature in objects thatare not observed at all. In KVO's this implementation, the object'sclass pointer is actually changed back to the original value (pointingto the original class) when enough observers are deregistered thatautomatic KVO notification does not need to be done at all anymore, forthat object. The second implementation involves leaving the object'sclass pointer alone, and mutating the original class' method list.

Referring to FIG. 6, a stylized representation of a method for providingan automatic key-value observing notification is shown in accordancewith one illustrative embodiment of the present invention. At block 600,a set of methods may be provided in a class of a second object (e.g.,the observed object 125). In the set of methods, at blocks 605, a methodwhose name follows a specific pattern may be located. The firstimplementation of the method may be replaced in block 610. At block 615,the second implementation of the method may be provided with in abilityto perform substantially the same operation as the first implementation.At an appropriate time, indicated by a decision block 620, in block 625a first method may be automatically invoked to indicate that a value ofa key of the second object will change. At a decision block 630, beforea value of a key changes, at block 635 an old value may be automaticallyprovided for the key. At an appropriate time, at block 640 a secondmethod may be automatically invoked to indicate that a value of the keydid change. At block 645, the new value may be automatically provided.

The observer object 120 may specify one or more options when itregisters as the observer of an observed object 125. However, one objectmay register another object as the observer of a property of a differentobject. In a first option, when the observer object 120 receives anotification from the observed object 125, a new value of a property,such as the manager property 315 may be obtained automatically from theobserved object 125. Optionally, even the old value of the property maybe obtained automatically in the notification, useful for example in anundo operation. For both the values of the property to be included inthe notification, the observed object 125 arranges for automaticinvoking of both the will change value for the key method 335 and didchange value for the key method 340 at an appropriate time.

For example, when the will change value for the key method 335 isinvoked, if any of the registered observers including the observerobject 120 desires the old value of the key before it changes,registered observers get the old value automatically and may berecorded. The key may be some identifier of the property, in oneembodiment. When the did change value for key method 340 is invoked, ifany of the registered observers seek to obtain the new value of the keyfor the property after the change, the registered observers get the newvalue automatically. In both the cases, using a value for key method ofthe key-value coding module 200, the will change and did change methods335, 340 provide the old and new values, respectively.

If multiple objects are related to an object, e.g., a plurality ofemployee objects are related to a manager object, after a change inrelationships between the employee objects and the manager object,specific employee objects which become related or unrelated to themanager object may be indicated in one embodiment. To this end, in anobserver notification a dictionary comprising information about thechanges may be included. This change information may include the old andnew values for a key of a property. The change information may indicatethe kind of change, e.g., whether the value for key is set or otheroptions, such as insertion, removal or replacement may be exercised. Forexample, when an object is related to a relatively large number of otherobjects, an addition of a single object at the end of the list of thehundred objects may be specifically indicated rather than indicatingthat a change involving one hundred one objects occurred in objectrelationships.

A class, such as an index set class which keeps track of a scattered setof indexes into an array may represent insertions of objects indifferent places in the array. When in a user interface, the indexedobjects are shown in a table view, a user may be able to select manydifferent objects in the list for removal and this removal of scatteredobjects may be indicated in a single observer notification exactlyindicating which objects were removed from the list.

Referring to FIG. 7, a stylized representation of a method forgenerating a single notification for multiple objects in accordance withone embodiment of the present invention is illustrated. At block 700,relationships of the observed object 125 to other objects may bedetermined. At a decision block 705, a check may be made to ascertainwhether a change in relationship has occurred. Thus, at the decisionblock 705, a change in relationship may be determined. That is, changein a relationship of the relationships of the observed object 125 toother objects may be ascertained. If a change in a relationship isindicated at the decision block 705, at a decision block 710, a check ismade to determine whether change in relationship has occurred in morethan one object. At block 715, a single notification may be generated,indicating a change in relationships of multiple objects.

When removing, for example, multiple rows from a table view where therows are scattered all across the table, only one observer notificationfrom the observed object 125 to the observer object 120 may be generatedinstead of generating one observer notification per row. In other words,the observed object 125 generates a single notification to the observerobject 120 for a change in status of some of the multiple objects. Thechange in status of some of the multiple objects includes at least on ofinsertion, removal and replacement of some of the objects in the to-manyrelationship.

Referring to FIG. 8, a stylized representation of a method for observinga property by observing an attribute identified by a key path isdepicted in accordance with one embodiment of the present invention. Atblock 800, an association of the observed object 125 with a class may bedetermined for the observer object 120. At block 805, an attribute ofthe observed object 125 may be determined to be a property of the class.A key path may be identified, linking the observed object 125 and theattribute, at block 810. In this manner, the property may be observed byobserving the attribute identified by the key path at block 815.

For example, a person class may have an address object associated as aninstance of an address class and a zip code may be an attribute of theaddress object. An observer can observe a person's address by observing“address.zipcode.” When the zip code of the address gets changed, theobserver of the address.zipcode may be notified. If the entire addressobject of the person is swapped out, all the observers of theaddress.zipcode may be notified. Thus, when properties get chained up asnested sub-properties, an automatic notification to all the observersmay be issued.

While the invention has been illustrated herein as being useful in anobject-oriented based computer environment, it also has application inother building block based programming environments. For example, thepresent invention may have application in any environment where two ormore programming building blocks or software entities are interconnectedand capable of communicating with one another.

The particular embodiments disclosed above are illustrative only, as theinvention may be modified and practiced in different but equivalentmanners apparent to those skilled in the art having the benefit of theteachings herein. Furthermore, no limitations are intended to thedetails of construction or design herein shown, other than as describedin the claims below. It is therefore evident that the particularembodiments disclosed above may be altered or modified and all suchvariations are considered within the scope and spirit of the invention.Accordingly, the protection sought herein is as set forth in the claimsbelow.

1. A method for providing an application programming interface in anobject-oriented platform, the method comprising: registering a firstobject with a second object to observe one or more properties thereof,said second object having a set of methods associated therewith, whereinregistering comprises passing a key path related to the one or moreproperties; and replacing a first implementation of a method in the setof methods of said second object with a second implementation of themethod that automatically invokes a notification to said first objectfrom said second object.
 2. A method, as set forth in claim 1, furthercomprising: providing the set of methods in a class of said secondobject; and locating the method whose name follows a specific pattern inthe set of methods of said second object.
 3. A method, as set forth inclaim 2, further comprising: providing the second implementation of themethod with an ability to perform substantially the same operation asthe first implementation of the method.
 4. A method, as set forth inclaim 1, wherein replacing a first implementation of a method furthercomprising: automatically invoking a first method at an appropriate timeto indicate that a valve for a key associated with said second objectwill change; and automatically invoking a second method at anappropriate time to indicate that the value for the key did change.
 5. Amethod, as set forth in claim 4, further comprising: invoking the firstimplementation of the method.
 6. A method, as set forth in claim 4,further comprising: after a change in the value, automatically providinga new value for the key of a property of said one or more properties tosaid first object from said second object.
 7. A method, as set forth inclaim 4, further comprising: before a change in the value, automaticallyproviding an old value for the key of a property of said one or moreproperties to said first object from said second object.
 8. A method, asset forth in claim 1, further comprising: determining relationships ofsaid second object to other objects; and in response to a change in arelationship between said second object and at least one of said otherobjects, indicating only the change in the relationship of said at leastone object and said second object within the notification.
 9. A method,as set forth in claim 8, further comprising: generating a singlenotification from said second object to said first object for a changein status of at least two objects of said other objects.
 10. A method,as set forth in claim 9, further comprising: causing the change instatus of said at least two objects of said other objects including atleast one of insertion, removal and replacement of said at least twoobjects.
 11. A method, as set forth in claim 2, further comprising:allowing the class of said second object to declare that a value of atleast one property of said one or more properties depends upon and getsaffected by a value of another property of said one or more properties.12. A method, as set forth in claim 1, further comprising: registeringan object class as an observer of a plurality of related objects in abatch that performs a common overhead associated with registration ofsaid plurality of related objects only once.
 13. A method, as set forthin claim 1, further comprising: deregistering an object class as anobserver of a plurality of related objects in a batch that performs acommon overhead associated with deregistration of said plurality ofrelated objects only once.
 14. A method, as set forth in claim 1,further comprising: determining an association of said second objectwith a class; determining an attribute of said second object being aproperty of said class; identifying a key path linking said secondobject and said attribute; and observing said property by observing theattribute identified by said key path.
 15. A method, as set forth inclaim 1, further comprising: upon registration of said first object,allowing said first object being observer of a property of said one ormore properties of said second object to be notified immediately of avalue of the observed property.
 16. A method, as set forth in claim 1,further comprising: causing one or more key-value observingnotifications to said first object in response to one or more key-valuecoding messages to said second object. 17.-32. (canceled)
 33. Anobject-oriented computing system, the system comprising: a processor; amemory coupled to said processor, said memory storing a data filestructure including an identification of a first and a second object ina software program, one or more properties and a set of methodsassociated with respective said first and second objects andinstructions for registering said first object with said second objectto observe said one or more properties thereof, wherein registeringcomprises passing a key path related to said one or more properties; anda user interface for enabling a user to replace a first implementationof a method in the set of methods of said second object with a secondimplementation of the method that automatically invokes a notificationto said first object from said second object.
 34. The object-orientedcomputing system of claim 33, wherein said software program is anoperating system program.
 35. The object-oriented computing system ofclaim 33, wherein said user interface comprises: a window that isdisplayed on a display device of the computing system.
 36. Theobject-oriented computing system of claim 33, wherein said data filestructure comprises: a database in which each object of said first andsecond object forms the basis for a record in the database, and eachrecord contains fields which respectively identify the pointers and themethods with which the record is associated. 37.-40. (canceled)